package com.educatioin.manage.wechat.controller;

import com.educatioin.manage.common.beans.Response;
import com.educatioin.manage.web.studytours.po.StudyToursContactPo;
import com.educatioin.manage.web.studytours.repository.StudyToursContactRepository;
import com.educatioin.manage.wechat.request.CreateContactInfoReq;
import com.educatioin.manage.wechat.response.GetContactInfoRsp;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zhangchen on 2018/4/20.
 */
@RestController
@RequestMapping("/wx/api/contact")
public class WeChatContactController {
    private static Logger logger = LoggerFactory.getLogger(WeChatContactController.class);
    @Resource
    private StudyToursContactRepository studyToursContactRepository;

    /**
     * 创建联系人信息接口
     * @param createContactInfoReq
     * @return
     */
    @RequestMapping("/createContactInfo")
    @ResponseBody
    public Response createContactInfo(@RequestBody CreateContactInfoReq createContactInfoReq) {
        Response response = new Response();
        response.setReturnCode(Response.RETURN_SUCCESS_CODE);
        //信息不完整
        if(StringUtils.isBlank(createContactInfoReq.getMobile())||StringUtils.isBlank(createContactInfoReq.getName())){
            response.setReturnCode(Response.RETURN_FAIL_CODE);
            response.setReturnMsg("输入信息不完整");
            return response;
        }

        //已存在
        StudyToursContactPo contactPo = studyToursContactRepository.findByNameAndMobile(createContactInfoReq.getName(),createContactInfoReq.getMobile());
        if(contactPo!=null){
            response.setReturnCode(Response.RETURN_SUCCESS_CODE);
            return response;
        }
        StudyToursContactPo studyToursContactPo = new StudyToursContactPo();
        studyToursContactPo.setName(createContactInfoReq.getName());
        studyToursContactPo.setMobile(createContactInfoReq.getMobile());
        studyToursContactRepository.save(studyToursContactPo);
        response.setReturnCode(Response.RETURN_SUCCESS_CODE);
        return response;
    }

    /**
     * 获取根据姓名查找联系人
     * @param name
     * @return
     */
    @RequestMapping("/getContactInfoByName")
    @ResponseBody
    public GetContactInfoRsp getContactInfoByName(String name) {
        GetContactInfoRsp getContactInfoRsp = new GetContactInfoRsp();

        List<StudyToursContactPo> studyToursContactPoList = studyToursContactRepository.findByName(name);
        if(studyToursContactPoList.isEmpty()){
            getContactInfoRsp.setReturnCode(Response.RETURN_FAIL_CODE);
            getContactInfoRsp.setReturnMsg("指定的联系人不存在！");
            return getContactInfoRsp;
        }
        getContactInfoRsp.setName(studyToursContactPoList.get(0).getName());
        getContactInfoRsp.setMobile(studyToursContactPoList.get(0).getMobile());
        return getContactInfoRsp;
    }

}
